package cn.lzx.web.controller;

import cn.lzx.query.PurchaseApplyQuery;
import cn.lzx.result.JSONResult;
import cn.lzx.result.PageList;
import cn.lzx.service.IPurchaseApplyService;
import cn.lzx.domain.PurchaseApply;

import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/purchaseApply")
public class PurchaseApplyController {

    @Autowired
    public IPurchaseApplyService purchaseApplyService;

    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JSONResult saveOrUpdate(@RequestBody PurchaseApply purchaseApply){
        if(purchaseApply.getId()!=null){
            purchaseApplyService.updateById(purchaseApply);
        }else{
            purchaseApplyService.insert(purchaseApply);
        }
        return JSONResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id){
        purchaseApplyService.deleteById(id);
        return JSONResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id")Long id){
        return JSONResult.success(purchaseApplyService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JSONResult list(){
        return JSONResult.success(purchaseApplyService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JSONResult page(@RequestBody PurchaseApplyQuery query){
        Page<PurchaseApply> page = new Page<PurchaseApply>(query.getPage(),query.getRows());
        page = purchaseApplyService.selectPage(page);
        return JSONResult.success(new PageList<PurchaseApply>(page.getTotal(),page.getRecords()));
    }
    @RequestMapping(value = "/approval/{applyId}/{status}",method = RequestMethod.GET)
    public JSONResult approval(@PathVariable("applyId") Long applyId, @PathVariable("status")Integer status){
        return JSONResult.success(purchaseApplyService.approval(applyId, status));
    }
}
